import 'package:flutter/material.dart';
import 'package:toastification/toastification.dart';

/// 优雅的自定义 Toast 组件
class ElegantToast {
  /// 显示小巧优雅的成功提示
  static void showSuccess(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    _showCustomToast(
      context: context,
      type: ToastificationType.success,
      title: title ?? '成功',
      message: message,
      primaryColor: const Color(0xFF10B981),
      backgroundColor: const Color(0xFFF0FDF4),
      icon: Icons.check_circle_rounded,
      duration: duration,
    );
  }

  /// 显示小巧优雅的错误提示
  static void showError(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    _showCustomToast(
      context: context,
      type: ToastificationType.error,
      title: title ?? '错误',
      message: message,
      primaryColor: const Color(0xFFEF4444),
      backgroundColor: const Color(0xFFFEF2F2),
      icon: Icons.error_rounded,
      duration: duration,
    );
  }

  /// 显示小巧优雅的警告提示
  static void showWarning(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    _showCustomToast(
      context: context,
      type: ToastificationType.warning,
      title: title ?? '警告',
      message: message,
      primaryColor: const Color(0xFFF59E0B),
      backgroundColor: const Color(0xFFFFFBEB),
      icon: Icons.warning_rounded,
      duration: duration,
    );
  }

  /// 显示小巧优雅的信息提示
  static void showInfo(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    _showCustomToast(
      context: context,
      type: ToastificationType.info,
      title: title ?? '提示',
      message: message,
      primaryColor: const Color(0xFF3B82F6),
      backgroundColor: const Color(0xFFEFF6FF),
      icon: Icons.info_rounded,
      duration: duration,
    );
  }

  /// 显示小巧优雅的加载提示
  static ToastificationItem showLoading(
    BuildContext context, {
    String message = '加载中...',
  }) {
    return toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.minimal,
      title: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            width: 16,
            height: 16,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation<Color>(
                const Color(0xFF3B82F6),
              ),
            ),
          ),
          const SizedBox(width: 8),
          Text(
            message,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Color(0xFF374151),
            ),
          ),
        ],
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: null, // 不自动关闭
      showProgressBar: false,
      closeOnClick: false,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(12),
      backgroundColor: Colors.white,
      foregroundColor: const Color(0xFF374151),
      borderSide: BorderSide(
        color: const Color(0xFFE5E7EB),
        width: 1,
      ),
      boxShadow: [
        BoxShadow(
          color: Colors.black.withOpacity(0.1),
          blurRadius: 20,
          offset: const Offset(0, 8),
          spreadRadius: 0,
        ),
      ],
      margin: const EdgeInsets.symmetric(horizontal: 0),  // 原 40 减去 40
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
    );
  }

  /// 核心的自定义 Toast 方法
  static void _showCustomToast({
    required BuildContext context,
    required ToastificationType type,
    required String title,
    required String message,
    required Color primaryColor,
    required Color backgroundColor,
    required IconData icon,
    Duration? duration,
  }) {
    toastification.show(
      context: context,
      type: type,
      style: ToastificationStyle.minimal,
      title: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 20,
            height: 20,
            decoration: BoxDecoration(
              color: primaryColor,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Icon(
              icon,
              size: 12,
              color: Colors.white,
            ),
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w600,
                    color: primaryColor,
                  ),
                ),
                if (message.isNotEmpty) ...[
                  const SizedBox(height: 2),
                  Text(
                    message,
                    style: const TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.w400,
                      color: Color(0xFF6B7280),
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: duration ?? const Duration(seconds: 3),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(12),
      backgroundColor: backgroundColor,
      foregroundColor: const Color(0xFF374151),
      borderSide: BorderSide(
        color: primaryColor.withOpacity(0.2),
        width: 1,
      ),
      boxShadow: [
        BoxShadow(
          color: primaryColor.withOpacity(0.15),
          blurRadius: 16,
          offset: const Offset(0, 4),
          spreadRadius: 0,
        ),
        BoxShadow(
          color: Colors.black.withOpacity(0.05),
          blurRadius: 6,
          offset: const Offset(0, 2),
          spreadRadius: 0,
        ),
      ],
      // 控制 Toast 的外边距（影响宽度）
      margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
      // 控制 Toast 内部内容的间距（影响实际显示高度）
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
    );
  }

  /// 显示极简的纯文字提示
  static void showSimpleText(
    BuildContext context, {
    required String text,
    Duration? duration,
  }) {
    toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.minimal,
      title: Text(
        text,
        style: const TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: Colors.white,
        ),
        textAlign: TextAlign.center,
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: duration ?? const Duration(seconds: 2),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(20),
      backgroundColor: const Color(0xFF374151).withOpacity(0.9),
      margin: const EdgeInsets.symmetric(horizontal: 0),  // 原 40 减去 40
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
    );
  }

  /// 显示自定义图标的优雅提示
  static void showCustomIcon(
    BuildContext context, {
    required IconData icon,
    required String message,
    Color? iconColor,
    Color? backgroundColor,
    Duration? duration,
  }) {
    final effectiveIconColor = iconColor ?? const Color(0xFF8B5CF6);
    final effectiveBackgroundColor = backgroundColor ?? const Color(0xFFF5F3FF);

    toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.minimal,
      title: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 24,
            height: 24,
            decoration: BoxDecoration(
              color: effectiveIconColor.withOpacity(0.1),
              borderRadius: BorderRadius.circular(6),
            ),
            child: Icon(
              icon,
              size: 16,
              color: effectiveIconColor,
            ),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Text(
              message,
              style: const TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
                color: Color(0xFF374151),
              ),
            ),
          ),
        ],
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: duration ?? const Duration(seconds: 3),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(10),
      backgroundColor: effectiveBackgroundColor,
      borderSide: BorderSide(
        color: effectiveIconColor.withOpacity(0.1),
        width: 1,
      ),
      boxShadow: [
        BoxShadow(
          color: effectiveIconColor.withOpacity(0.1),
          blurRadius: 12,
          offset: const Offset(0, 4),
          spreadRadius: 0,
        ),
      ],
      margin: const EdgeInsets.symmetric(horizontal: 0, vertical: 8),  // 原 16 减去 16
      padding: const EdgeInsets.all(12),
    );
  }

  /// 显示固定宽度的 Toast
  static void showFixedWidth(
    BuildContext context, {
    required String message,
    double width = 80,   // 原 200 减 120
    double? height,
  }) {
    toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.minimal,
      title: Container(
        width: width,
        height: height,
        child: Text(
          message,
          style: const TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.w500,
            color: Color(0xFF374151),
          ),
          textAlign: TextAlign.center,
        ),
      ),
      alignment: Alignment.topCenter,
      autoCloseDuration: const Duration(seconds: 3),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(12),
      backgroundColor: Colors.white,
      borderSide: const BorderSide(
        color: Color(0xFFE5E7EB),
        width: 1,
      ),
      boxShadow: [
        BoxShadow(
          color: Colors.black.withOpacity(0.1),
          blurRadius: 16,
          offset: const Offset(0, 4),
          spreadRadius: 0,
        ),
      ],
      // 通过设置很小的 margin 来让内容决定最终宽度
      margin: const EdgeInsets.symmetric(horizontal: 0),
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
    );
  }

  /// 显示紧凑型 Toast（小尺寸）
  static void showCompact(
    BuildContext context, {
    required String message,
    ToastificationType type = ToastificationType.info,
  }) {
    Color primaryColor;
    IconData icon;
    
    switch (type) {
      case ToastificationType.success:
        primaryColor = const Color(0xFF10B981);
        icon = Icons.check;
        break;
      case ToastificationType.error:
        primaryColor = const Color(0xFFEF4444);
        icon = Icons.close;
        break;
      case ToastificationType.warning:
        primaryColor = const Color(0xFFF59E0B);
        icon = Icons.warning;
        break;
      default:
        primaryColor = const Color(0xFF3B82F6);
        icon = Icons.info;
    }

    toastification.show(
      context: context,
      type: type,
      style: ToastificationStyle.minimal,
      title: Container(
        constraints: const BoxConstraints(
          minWidth: 80,   // 原 200 减 120
          maxWidth: 140,  // 原 260 减 120
          minHeight: 28,
          maxHeight: 40,
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              icon,
              size: 12,
              color: primaryColor,
            ),
            const SizedBox(width: 6),
            Flexible(
              child: Text(
                message,
                style: TextStyle(
                  fontSize: 11,
                  fontWeight: FontWeight.w500,
                  color: primaryColor,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: const Duration(seconds: 2),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(6),
      backgroundColor: primaryColor.withOpacity(0.1),
      borderSide: BorderSide(
        color: primaryColor.withOpacity(0.3),
        width: 1,
      ),
      // 更紧凑的边距和内边距
      margin: const EdgeInsets.symmetric(horizontal: 0, vertical: 4),  // 原 80 减去 80
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
    );
  }

  /// 显示超宽 Toast（全屏宽度）
  static void showFullWidth(
    BuildContext context, {
    required String message,
    String? title,
  }) {
    toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.minimal,
      title: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          if (title != null) ...[
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Color(0xFF374151),
              ),
            ),
            const SizedBox(height: 4),
          ],
          Text(
            message,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w400,
              color: Color(0xFF6B7280),
            ),
          ),
        ],
      ),
      alignment: Alignment.topCenter,
      autoCloseDuration: const Duration(seconds: 4),
      showProgressBar: true,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.onHover,
      borderRadius: BorderRadius.circular(0),
      backgroundColor: const Color(0xFF1F2937),
      foregroundColor: Colors.white,
      // 全屏宽度：margin 设为 0
      margin: const EdgeInsets.all(0),
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
    );
  }

  /// 显示超小型 Toast（极简尺寸）
  static void showMini(
    BuildContext context, {
    required String message,
    ToastificationType type = ToastificationType.info,
  }) {
    Color primaryColor;
    IconData icon;
    
    switch (type) {
      case ToastificationType.success:
        primaryColor = const Color(0xFF10B981);
        icon = Icons.check;
        break;
      case ToastificationType.error:
        primaryColor = const Color(0xFFEF4444);
        icon = Icons.close;
        break;
      case ToastificationType.warning:
        primaryColor = const Color(0xFFF59E0B);
        icon = Icons.warning;
        break;
      default:
        primaryColor = const Color(0xFF3B82F6);
        icon = Icons.info;
    }

    toastification.show(
      context: context,
      type: type,
      style: ToastificationStyle.minimal,
      title: Container(
        constraints: const BoxConstraints(
          minWidth: 60,   // 原 180 减 120  
          maxWidth: 100,  // 原 220 减 120
          minHeight: 24,
          maxHeight: 32,
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              icon,
              size: 10,
              color: primaryColor,
            ),
            const SizedBox(width: 4),
            Flexible(
              child: Text(
                message,
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.w600,
                  color: primaryColor,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.center,
              ),
            ),
          ],
        ),
      ),
      alignment: Alignment.topRight,
      autoCloseDuration: const Duration(seconds: 1, milliseconds: 500),
      showProgressBar: false,
      closeOnClick: true,
      closeButtonShowType: CloseButtonShowType.none,
      borderRadius: BorderRadius.circular(16),
      backgroundColor: primaryColor.withOpacity(0.08),
      borderSide: BorderSide(
        color: primaryColor.withOpacity(0.2),
        width: 0.5,
      ),
      // 超小的边距和内边距
      margin: const EdgeInsets.symmetric(horizontal: 0, vertical: 2),  // 原 100 减去 100
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
    );
  }
}
